#include "stdafx.h"
#include "KeyboardInput.hpp"

std::unordered_map<sf::Keyboard::Key, rh::KeyState, std::hash<int>> rh::KeyboardInput::_keyStateMap;

rh::KeyboardInput::KeyboardInput(void)
{
}


rh::KeyboardInput::~KeyboardInput(void)
{
}

void rh::KeyboardInput::RegisterKey(const sf::Keyboard::Key& key)
{
	_keyStateMap.insert(std::pair<sf::Keyboard::Key, rh::KeyState>(key, KeyState::Inactive));
}

void rh::KeyboardInput::UnregisterKey(const sf::Keyboard::Key& key)
{
	_keyStateMap.erase(key);
}

void rh::KeyboardInput::Update(const sf::Time& elapsedTime, const sf::Time& totalElapsedTime)
{
	for(KeyMap::iterator i = _keyStateMap.begin(); i != _keyStateMap.end(); i++)
	{
		bool isKeyPressed = sf::Keyboard::isKeyPressed(i->first);
		KeyState previousState = i->second;
		if(isKeyPressed)
		{
			if(previousState == KeyState::Inactive || previousState == KeyState::Released)
				i->second = KeyState::Pressed;
			else
				i->second = KeyState::Hold;
		}
		else
		{
			if(previousState == KeyState::Hold || previousState == KeyState::Pressed)
				i->second = KeyState::Released;
			else
				i->second = KeyState::Inactive;
		}
	}
}

rh::KeyState rh::KeyboardInput::GetKeyState(sf::Keyboard::Key key)
{
	return _keyStateMap[key];
}